package gui.newWindows;

import gui.Main.GUI;

import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import phase6.PortNumber;

public class ChooseServerPanel extends JPanel
{
	protected GUI main;
	protected Window window;
	protected JLabel ipLabel;
	protected JLabel portLabel;
	protected JTextField ipField;
	protected JTextField portField;
	protected JButton connectButton;

	public ChooseServerPanel(GUI main, Window window)
	{
		super(new GridLayout(3, 2));
		this.main = main;
		this.window = window;

		ipLabel = new JLabel("IP Address");
		portLabel = new JLabel("Port");
		ipField = new JTextField("127.0.0.1", 20);
		portField = new JTextField(phase6.SegmentRunner.DEFAULT_PORT + "", 10);

		this.add(ipLabel);
		this.add(ipField);
		this.add(portLabel);
		this.add(portField);

		connectButton = new JButton("Connect");
		connectButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				connectButtonPressed();
			}
		});

		this.add(connectButton);
	}

	protected void connectButtonPressed()
	{
		PortNumber port;
		try
		{
			port = new PortNumber(Integer.parseInt(portField.getText()));

		}
		catch (NumberFormatException e)
		{
			// TODO
			e.printStackTrace();
			port = new PortNumber(phase6.SegmentRunner.DEFAULT_PORT);
		}
		main.joinSession(ipField.getText(), port);
		window.setVisible(false);
		window.dispose();
	}

}
